package com.sat.order.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OrderRabbitMqConfig {
    public static final String TOPIC_EXCHANGE = "order_topic_exchange";
    //队列名称
    public static final String QUEUE = "order_queue";
    public static final String Dl_QUEUE = "order_dl_queue";
    public static final String DL_TOPIC_EXCHANGE = "order_topic_exchange";

    @Bean("orderTopicExchange")
    public Exchange topicExchange(){
        return ExchangeBuilder.topicExchange(TOPIC_EXCHANGE).durable(true).build();
    }

    @Bean("orderQueue")
    public Queue topicQueue(){
        return QueueBuilder.durable(QUEUE)
                .withArgument("x-message-ttl", 60000)//设置过期时间
                //配置死信交换机
                .withArgument("x-dead-letter-exchange","order_dl_topic_exchange")
                //配置死信交换机的消息发送规则
                .withArgument("x-dead-letter-routing-key","order.dl.sat.wgl")
                .build();
    }

    @Bean
    public Binding itemQueueExchange(
            @Qualifier("orderQueue")Queue queue,
            @Qualifier("orderTopicExchange")Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("order.sat.#").noargs();
    }
    //配置死信交换机
    @Bean("orderDlTopicExchange")
    public Exchange wglexchange(){
        return ExchangeBuilder.topicExchange("order_dl_topic_exchange").durable(true).build();
    }

    @Bean("orderDlQueue")
    public Queue wglqueuedlx(){
        return QueueBuilder.durable("order_dl_queue").build();
    }

    @Bean
    public Binding dlqueueExchangeDlx(
            @Qualifier("orderDlTopicExchange") Exchange exchange,
            @Qualifier("orderDlQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("order.dl.sat.#").noargs();
    }
}
